package com.usiful.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.locks.ReentrantLock;

import com.usiful.model.ActionArg;

/**
 * 控制器基类
 * 
 * @author [周织能]
 *
 */
public class BaseController {

	public interface HandleCallback {
		public boolean prevHandle();

		public void succ();

		public void fail(String msg);
	}

	protected HandleCallback mHandleCb;

	public BaseController() {
	}

	public boolean handleAction(ActionArg arg) {
		if (mHandleCb != null) {
			mHandleCb.prevHandle();
		}
		if(arg.getArgValueMap().containsKey(ActionArg.HELP)) {
			help(arg);
		} else {
			if(!checkArgNecessity(arg)) {
				System.exit(1);
			}
		}
		return true;
	}

	/**
	 * 检查必要的参数是否缺失,对特殊情况下required的一律当option，若要再鉴定，需子类自行实现
	 * @param arg
	 */
	protected boolean checkArgNecessity(ActionArg arg) {
		boolean isCheckOk = true;
		Map<String,Boolean> lackArgs = new HashMap<String,Boolean>();
		Map<String,String> argParamDescSet = arg.getArgParamDescSet();	//参数的描述字符
		Set<String> descKeySet = argParamDescSet.keySet();
		String descStr = null;
		for(String descKey : descKeySet) {
			descStr = argParamDescSet.get(descKey);
			if(descStr.contains("[required]")) {
				lackArgs.put(descKey,true);
			}
		}
		Set<String> keySet = arg.getArgValueMap().keySet();	//所有参数的名称
		for(String key : keySet) {
			if(lackArgs.containsKey(key)) {
				lackArgs.remove(key);
			}
		}
		StringBuffer lackParams = new StringBuffer();
		Set<String> lackKeySet = lackArgs.keySet();
		for(int i = 0; i < lackKeySet.size(); i++) {
			lackParams.append(lackKeySet.iterator().next());
			if(i != lackKeySet.size() - 1) {
				lackParams.append(",");
			}
		}
		if(!lackArgs.isEmpty()) {
			System.out.println("请输入参数: --" + lackParams.toString() + "及其值!");
			//System.exit(1);
			isCheckOk = false;
		} else {
			System.out.println("参数检测完毕...");
			isCheckOk = true;
		}
		return isCheckOk;
	}
	
	protected void help(ActionArg arg) {
		Map<String,String> paramDescSet = arg.getArgParamDescSet();
		if(paramDescSet != null && paramDescSet.size() > 0) {
			StringBuffer helpStr = new StringBuffer();
			helpStr.append("\t************************************************************************  \n");
			helpStr.append("\t\t  " + arg.getActionName() + " 命令支持参数如下: \n");
			Set<String> keySet = paramDescSet.keySet();
			for(String key : keySet) {
				helpStr.append("\t________________________________________________________________________  \n");
				helpStr.append("\t\t --" + key + ":" + paramDescSet.get(key) + "\t\n");
				
			}
			helpStr.append("\t*************************************************************************  \n");
			System.out.println(helpStr);
		}
	}
	
	/**
	 * 补全空格
	 * @param count
	 * @return
	 */
	private String completeSpaceChar(String str) {
		int count = str.length();
		int l = 40 > count ? 40 - count : 0;
		StringBuffer sb = new StringBuffer();
		System.out.println(count);
		System.out.println(l);
		for(int i = 0; i < l; i++) {
			sb.append("*");
		}
		System.out.println(str.length() + sb.toString().length());
		return str + sb.toString();
	}
	
	public void handleActions(List<ActionArg> args) {
	}

	public void setHandleCallback(HandleCallback handleCb) {
		mHandleCb = handleCb;
	}

	public HandleCallback getHandleCallback() {
		return mHandleCb;
	}

	
}
